// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
import catctl = require("../objctl/lobbycat");
import utils = require("../utils/utils")
enum PStatus { Kitchen, Lobby, Frontier,Moving}
@ccclass
export default class HouseSceneMgr extends cc.Component {

    @property(cc.Node)
    lobbyStandPos:cc.Node
    @property(cc.Node)
    kitchenStandPos:cc.Node
    @property(cc.Node)
    frontStandPos:cc.Node

    @property(cc.Node)
    kitchenRoom:cc.Node
    @property(cc.Node)
    lobbyRoom:cc.Node
    @property(cc.Node)
    frontRoom:cc.Node

    @property(cc.ScrollView)
    receiptList:cc.ScrollView
    @property(cc.Button)
    receiptBtn: cc.Button

    @property(cc.Button)
    forwardBtn: cc.Button
    forwardTxt: cc.Label

    @property(cc.Button)
    backwardBtn:cc.Button
    backwardTxt: cc.Label

    

    @property(catctl.default)
    catObj: catctl.default = null

    catPStatus: PStatus
    
    onLoad()
    {
        
    }

    start () {
        this.forwardTxt = cc.find("/Background/Label", this.forwardBtn.node).getComponent(cc.Label)
        this.backwardTxt = cc.find("/Background/Label", this.backwardBtn.node).getComponent(cc.Label)
        this.changePStatus(PStatus.Lobby)
    }

    private hideForwardBtn(){
        this.forwardBtn.node.active = false
    }
    private showForwardBtn(){
        this.forwardBtn.node.active = true
    }
    private hideBackwardBtn(){
        this.backwardBtn.node.active = false
    }
    private showBackwardBtn(){
        this.backwardBtn.node.active = true
    }

    private updateUI(){
        switch (this.catPStatus) {
            case PStatus.Kitchen:
                this.showForwardBtn()
                this.forwardTxt.string = "去客厅"
                break;
            case PStatus.Lobby:
                this.showForwardBtn()
                this.showBackwardBtn()
                this.forwardTxt.string = "去玄关"
                this.backwardTxt.string = "去厨房"
                break;
            case PStatus.Frontier:
                this.hideForwardBtn()
                this.forwardTxt.string = "去钓鱼"
                this.backwardTxt.string = "去大厅"
                break;
            case PStatus.Moving:
                this.hideBackwardBtn()
                this.hideForwardBtn()
                break
        }
    }

    private changePStatus(newStatus:PStatus){
        if (this.catPStatus == newStatus)
        {
            return
        }
        this.catPStatus = newStatus
        this.updateUI()
    }
    /**
     * MoveForwad
     */
    public MoveForward() 
    {
        var targetPos:cc.Vec2
        var newStatus:PStatus
        //根据自身当前站的位置,来判断前进到哪个房间
        switch (this.catPStatus) {
            case PStatus.Frontier:
                cc.log("玄关无法在向前走了")
                // targetPos = utils.Util.ConvertToNodePos(this.frontStandPos,this.node)
                break;
            case PStatus.Kitchen:
                targetPos = utils.Util.ConvertToNodePos(this.lobbyStandPos, this.node)
                newStatus = PStatus.Lobby
                break;
            case PStatus.Lobby:
                targetPos = utils.Util.ConvertToNodePos(this.frontStandPos, this.node)
                newStatus = PStatus.Frontier
                break
        }
        cc.tween(this.catObj.node)
        .to(3, { position: targetPos})
        .call(()=>{this.changePStatus(newStatus)})
        .start()
    }

    public MoveBackward()
    {
        var targetPos: cc.Vec2
        var newStatus: PStatus
        //根据自身当前站的位置,来判断后退到哪个房间
        switch (this.catPStatus) {
            case PStatus.Frontier:
                targetPos = utils.Util.ConvertToNodePos(this.lobbyStandPos, this.node)
                newStatus = PStatus.Lobby
                break;
            case PStatus.Kitchen:
                cc.log("厨房无法在向前走了")
                break;
            case PStatus.Lobby:
                targetPos = utils.Util.ConvertToNodePos(this.kitchenStandPos, this.node)
                newStatus = PStatus.Kitchen
                break
        }
        cc.tween(this.catObj.node)
            .to(3, { position: targetPos })
            .call(() => { this.changePStatus(newStatus) })
            .start()
    }
    
    public ClickMoveBack(evt:cc.Event) 
    {
        this.MoveBackward()   
    }

    public ClickMoveForward(evt:cc.Event)
    {
        this.MoveForward()
    }

    public ClickReceipt(evt:cc.Event)
    {

    }

    public OnScrollEvent(scrollview: cc.ScrollView, eventType: cc.ScrollView.EventType, CustomEventData:string)
    {
        cc.log("eventType = ", eventType, " CustomEventData = ", CustomEventData)
    }
    // update (dt) {}
}
